<?
if (isset($_POST['btnsubmit'])) {
    require("required/dbconnect.php");

    $totalitem = $_POST["totalitem"];
    $id = $_POST['hidid'];
    $remarks = $_POST['remarks'];
    $system_remark = "";

    for ($u = 1; $u < $totalitem + 1; $u++) {
        $desc = $_POST['desc' . $u];

        if ($desc == "") { //delete
            $itemid = $_POST['itemid' . $u];
            $current_desc = $_POST['current_desc' . $u];
            $delete = mysql_query("delete from os_product_inventory where id=$itemid");
            $system_remark = "$current_desc has been deleted";
            $quantity = 0;
        } else {

            
            $unit_price = $_POST['unit_price' . $u];


            if (empty($_POST['itemid' . $u])) {
                $insert_item = "insert into os_product_inventory(pid,description,quantity,unit_price,promotion_price,retail_price)values
								  ($id,'$desc','$quantity','$unit_price','$promotion_price','$retail_price')";
                $insert_item_query = mysql_query($insert_item) or die(mysql_error());
                $system_remark.="Insert new item [$desc] with quantity = $quantity and unit price = $unit_price<br>";
            } else {
               
                $current_desc = $_POST['current_desc' . $u];
                $current_price = $_POST['current_price' . $u];


                if ($desc != $current_desc) {
                    $system_remark.="[$current_desc] has been rename to [$desc]<br>";
                }
                if ($unit_price != $current_price) {
                    $system_remark.="The price for [$desc] has been changed from $current_price to $unit_price<br>";
                }



               

                $itemid = $_POST['itemid' . $u];
                $update_item = "update os_product_inventory set description='$desc',unit_price='$unit_price' where id=$itemid";
                $update_item_query = mysql_query($update_item) or die(mysql_error());
                //echo $update_item."<br>";
            }
        }
    }

    $insert = mysql_query("insert into os_stockrecord (pid,user_remarks,system_remarks,add_datetime)values($id,'$remarks','$system_remark',now())") or die(mysql_error());

    header("Location:product_viewfull.php?id=$id&updated=3");
} else {

    require("required/dbconnect.php");
    require("required/function.php");
    require("required/header.php");

    checkAccess("allow_product_edit", 1);
    $id = $_GET['id'];
    $get_id = mysql_query("select * from os_products where id=$id");

    $a = mysql_fetch_array($get_id, MYSQL_ASSOC);
    ?>
    <script language="javascript">
        function validatefrm(frm)
        {
            if(document.frm.stock_amount.value=="")
            {
                alert("Please select total stock");
                document.frm.stock_amount.focus();
                return false;
            }
        		
            return true;
        	
        }
    </script>
    <table width="100%"  cellpadding="0" cellspacing="5" height="300">
        <script language="JavaScript" type="text/javascript" src="wysiwyg.js">
        </script>
        <script language="JavaScript" type="text/javascript" src="required/js/select_cat.js"></script>

        <td width="84%" align="left" valign="top">
            <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post" onSubmit="return validatefrm(this)">
                <table width="100%">
                    <td class="title" colspan="2"><strong>Update Stock</strong></td>

                    <tr valign="top">
                        <td width="11%" colspan="2">Current Stock Available for <strong><?= $a['product_name_eng'] ?></strong>
                            <br>
                            Note : To delete a record, kindly leave the description to empty. <br>
                            <font color="#FF0000">WARNING! This might cause error to invoices that contained this item.</font>


                        </td>
                    </tr>
                    <tr>
                    <script type="text/javascript">
                        function addFloat(elems)
                        {
                            var value1=parseFloat(document.getElementById(elems).value);
                            document.getElementById(elems).value=value1.toFixed(2);
                        }
                        function addElement2() {
                            var ni = document.getElementById('myDiv');
                            var numi = document.getElementById('theValue');
                            var num = (document.getElementById('theValue').value -1)+ 2;
                            numi.value = num;
                            var newdiv = document.createElement('div');
                            var divIdName = 'description'+num;
                            newdiv.setAttribute('id',divIdName);
                            newdiv.innerHTML = '<tr><td>Description '+num+' <input type="text" name="desc'+num+'" class="contentfont" size=20> &nbsp;&nbsp;&nbsp;&nbsp;'+ 
                                
                                'Unit Price <input type="text" name="unit_price'+num+'" class="contentfont" size=5 maxlength="10">  &nbsp;&nbsp;';
                                   
                            ni.appendChild(newdiv);
                            document.getElementById("myDiv2").innerHTML="<input type=hidden value='"+num+"' name=totalitem>";
                        }
                    </script>	
                    <?
                    $get_stock = mysql_query("select * from os_product_inventory where pid=" . $a['id']);
                    $countitem = mysql_num_rows($get_stock);
                    ?>
                    <td colspan="2">
                        <table>
                            <?
                            if (mysql_num_rows($get_stock) > 0) {
                                $u = 1;
                                while ($s = mysql_fetch_array($get_stock, MYSQL_ASSOC)) {
                                    ?>
                                    <tr>
                                        <td>
                                            Description <?= $u ?> <input type="text" size="20" value="<?= htmlspecialchars($s['description']) ?>" name="desc<?= $u ?>"> &nbsp;&nbsp;
                                           
                                            Unit Price <input type="text" size="5" maxlength="10" value="<?= $s['unit_price'] ?>" name="unit_price<?= $u ?>"> &nbsp;&nbsp;


                                        </td>
                                    </tr>
                                    <input type="hidden" value="<?= $s['id'] ?>" name="itemid<?= $u ?>">
                                    <input type="hidden" value="<?= $s['description'] ?>" name="current_desc<?= $u ?>">
                                    <input type="hidden" value="<?= $s['unit_price'] ?>" name="current_price<?= $u ?>">

                                  
                                    <?
                                    $u++;
                                }
                            }
                            ?>

                            <tr>
                                <td>
                                    <input type="hidden" value="<?= $countitem ?>" id="theValue" />
                                    <div id="myDiv"></div>
                                    <div id="myDiv2"><input type=hidden value='<?= $countitem ?>' name=totalitem></div>
                                    <p><a href=# onclick="addElement2()">Add New Item</a></p>
                                </td>
                            </tr>
                        </table>		

                    </td>
                    </tr>
                    <tr>
                        <Td>Remark</Td><td><input type="text" size="50" name="remarks"></td>
                    </tr>


                    <input type="hidden" value="<?= $id ?>" name="hidid">
                    <tr>
                        <td colspan="2"><input type="submit" class="button" value="Update" name="btnsubmit"> <input type="button" onClick="history.go(-1)" class="button" value="Back"></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <br><br>
                            <hr>
                            <div class="sub_title">Stock Update History</div>
                            <table width="100%">
                                <tr class="table_header">
                                    <td width="3%">No</td><td width="47%">Updated Stock</td><Td width="34%">Remarks</Td><td width="16%">Date</td>
                                </tr>
                                <?
                                $get = mysql_query("select * from os_stockrecord where pid=$id order by add_datetime desc") or die(mysql_error());

                                if (mysql_num_rows($get) == 0) {
                                    ?>
                                    <Tr>
                                        <td colspan="4" align="center"> - no record - </td>
                                    </Tr>
                                    <?
                                } else {
                                    $no = 1;
                                    while ($b = mysql_fetch_array($get, MYSQL_ASSOC)) {
                                        if ($no % 2 == 0) {
                                            $bgcolor = "#e5e5e5";
                                        } else {
                                            $bgcolor = "#eeeeee";
                                        }
                                        ?>
                                        <tr height="20" valign="top" bgcolor="<?= $bgcolor ?>" onmouseover="this.style.backgroundColor='ccccff'" onmouseout="this.style.backgroundColor='<?= $bgcolor ?>'">
                                            <td><?= $no ?></td>
                                            <td><?= $b['system_remarks'] ?></td>
                                            <td><?= $b['user_remarks'] ?></td>
                                            <td><?= $b['add_datetime'] ?></td>
                                        </tr>

                                        <?
                                        $no++;
                                    }
                                }
                                ?>

                            </table>

                        </td>
                    </tr>

                </table>
            </form>
        </td>

    </table>
<? }require("required/footer.php"); ?>